﻿using System;
using System.IO;
using Microsoft.AspNetCore.StaticFiles;
using studentSubsidy.Dao;
using studentSubsidy.Entity;
using studentSubsidy.Models;
using studentSubsidy.Utils;

namespace studentSubsidy.Service
{
    public class UploadService
    {
        private UploadDao _uploadDao = new UploadDao();

        public void addUploadFile(string fileName, string fileExtension, int id, int product_id)
        {
            _uploadDao.insertUploadFile(fileName, fileExtension, id, product_id);
        }

        public int UpLoadDelete(string fileName)
        {
            try
            {
                string path = "F://Projects/.NET/studentSubsidy/studentSubsidy/Public/file/" + fileName;
                if (string.IsNullOrEmpty(path))
                {
                    return 400;
                }

                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                    return 200;
                }
                else
                {
                    return 400;
                }
            }
            catch (Exception ex)
            {
                return 400;
            }
        }

        public Result addSignedFile(string fileName, string fileExtension, int id, int product_id)
        {
            Signed_File signedFile = new Signed_File
            {
                id = GenerateID.GenForInt(),
                apply_id = product_id,
                update_id = id,
                isCheck = 1,
                filePath = fileName,
                fileType = 0
            };
            _uploadDao.insertSignedFile(signedFile);
            return new Result(200, "插入成功", signedFile.filePath, signedFile.id);
        }

        public Result addSignedPicFile(string fileName,int type, int userId, int ApplyId)
        {
            int isCheck = 0;
            if (type == 1)
            {
                isCheck = 1;
                _uploadDao.updateSignedStatus(ApplyId);
            }
            Signed_File signedFile = new Signed_File
            {
                id = GenerateID.GenForInt(),
                update_id = userId,
                filePath = fileName,
                fileType = type,
                isCheck = isCheck,
                apply_id = ApplyId
            };
            _uploadDao.insertSignedFile(signedFile);
            return new Result(200, "插入成功", signedFile.filePath, signedFile.id);
        }
    }
}